package switchviz.core.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;
import switchviz.graphic.gauge.GaugeNodeModel;
import switchviz.gui.component.JLabeldComponent;

public class ViewNewGaugeDialog extends JDialog implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6599864970352781760L;
	private JLabeldComponent _cmbgaugelist = null;
	private JLabeldComponent _txtgaugename = null;
	private JButton _buttaddgauge = null;
	private JButton _buttcancel = null;
	private GaugeNodeModel _gnm = null;
	private static ViewNewGaugeDialog _instance = null;
	
	private ViewNewGaugeDialog()
	{
		this.setSize(new Dimension(500, 100));
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		initialize();
	}
	
	public static ViewNewGaugeDialog getInstance()
	{
		if(_instance != null)
			_instance.setVisible(false);
		_instance = new ViewNewGaugeDialog();
		return _instance;
	}
	
	private void initialize()
	{
		_cmbgaugelist = new JLabeldComponent("Add Gauge:   Type: ", JLabeldComponent.COMBOBOX);
		_cmbgaugelist.getComboBox().addItem("Tacho3d");
		_cmbgaugelist.getComboBox().addItem("Graph2d");
		_cmbgaugelist.getComboBox().addItem("BarGraph");
		_txtgaugename = new JLabeldComponent("Gauge Name: ", JLabeldComponent.TEXTFIELD);
		_txtgaugename.getTextField().setColumns(10);
		_buttaddgauge = new JButton("Add");
		_buttaddgauge.setActionCommand(ResourceManager.getInstance().getActionAddItem());
		_buttcancel = new JButton("Cancel");
		_buttcancel.addActionListener(this);
		
		JPanel buttpanel = new JPanel();
		buttpanel.add(_buttaddgauge);
		buttpanel.add(_buttcancel);
		
		JPanel editpanel = new JPanel();
		editpanel.add(_cmbgaugelist);
		editpanel.add(_txtgaugename);
		
		JPanel paneladdgauge = new JPanel();
		paneladdgauge.setLayout(new BoxLayout(paneladdgauge, BoxLayout.PAGE_AXIS));
		paneladdgauge.add(editpanel);
		paneladdgauge.add(buttpanel);
		
		getContentPane().add(paneladdgauge);
	}
	
	public GaugeNodeModel getGaugeNodeModel()
	{
		if(_gnm == null)
			_gnm = new GaugeNodeModel(_txtgaugename.getText(), _cmbgaugelist.getText(), null);
		return _gnm;
	}
	
	public void setGaugeNodeModel(GaugeNodeModel gnm)
	{
		_gnm = gnm;
	}
	
	public void addActionListener(ActionListener l)
	{
		_buttaddgauge.addActionListener(l);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		this.setVisible(false);
	}
}
